package view;
/**
 *
 * @author zhaoyingxi
 */
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.SystemColor;
import Control.UserControl;
import model.User;
import java.awt.Color;


public class RegisterViewT extends JFrame {
    public RegisterViewT() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    JLabel jLabel1 = new JLabel();
    JTextField userNameText = new JTextField();
    JTextField phoneText = new JTextField();
    JLabel jLabel2 = new JLabel();
    JLabel jLabel4 = new JLabel();
    JPasswordField passWordText = new JPasswordField();
    JLabel jLabel5 = new JLabel();
    JPasswordField repassWordText = new JPasswordField();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    JLabel errorLabel = new JLabel();

    private void jbInit() throws Exception {
        getContentPane().setLayout(null);
        this.getContentPane().setBackground(SystemColor.scrollbar);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setResizable(false);
        this.setTitle("User Register");
        jLabel1.setText("Username:");
        jLabel1.setBounds(new Rectangle(108, 40, 91, 15));
        passWordText.setBounds(new Rectangle(230, 74, 112, 20));
        jLabel4.setText("Password:");
        jLabel4.setBounds(new Rectangle(107, 80, 88, 15));
        jLabel2.setText("Phone:");
        jLabel2.setBounds(new Rectangle(106, 170, 68, 15));
        phoneText.setBounds(new Rectangle(230, 165, 112, 20));
        repassWordText.setBounds(new Rectangle(230, 118, 112, 20));
        jLabel5.setText("RePassword:");
        jLabel5.setBounds(new Rectangle(107, 124, 95, 15));
        jButton2.addActionListener(new RegisterViewT_jButton2_actionAdapter(this));
        errorLabel.setForeground(Color.red);
        errorLabel.setBounds(new Rectangle(176, 258, 85, 15));
        this.getContentPane().add(repassWordText);
        jButton2.setBounds(new Rectangle(235, 218, 81, 23));
        jButton2.setText("Cancel");
        jButton1.setBounds(new Rectangle(107, 219, 81, 23));
        jButton1.setText("Submit");
        jButton1.addActionListener(new RegisterViewT_jButton1_actionAdapter(this));
        this.getContentPane().add(jLabel1);
        this.getContentPane().add(userNameText);
        this.getContentPane().add(jLabel4);
        this.getContentPane().add(passWordText);
        this.getContentPane().add(jLabel5);
        this.getContentPane().add(jLabel2);
        this.getContentPane().add(phoneText);
        this.getContentPane().add(jButton1);
        this.getContentPane().add(jButton2);
        this.getContentPane().add(errorLabel);
        userNameText.setBounds(new Rectangle(232, 35, 112, 20));
        setBounds(300,300,450,300);
        setVisible(true);
    }
    //submit
    public void jButton1_actionPerformed(ActionEvent e) {
        User user = new User();
        if(passWordText.getText().trim().equalsIgnoreCase(repassWordText.getText().trim()))
        {
            user.setLoginName(userNameText.getText());
            user.setPassWord(passWordText.getText());
            user.setPhone(phoneText.getText());
            user.setStatus("0");
            System.out.println(userNameText.getText());
            System.out.println(phoneText.getText());
            UserControl.register(user);
            this.dispose();
       }
       else{
           errorLabel.setText("Error");
       }
    }
    //cancel
    public void jButton2_actionPerformed(ActionEvent e) {
        this.dispose();
    }
    public static void main(String[] args){
        RegisterViewT a = new RegisterViewT();

    }
}
class RegisterViewT_jButton2_actionAdapter implements ActionListener {
    private RegisterViewT adaptee;
    RegisterViewT_jButton2_actionAdapter(RegisterViewT adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton2_actionPerformed(e);
    }
}


class RegisterViewT_jButton1_actionAdapter implements ActionListener {
    private RegisterViewT adaptee;
    RegisterViewT_jButton1_actionAdapter(RegisterViewT adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
}
